package com.example.controller;

import com.example.dto.LoginDto;
import com.example.utils.JWTUtils;
import com.example.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @PostMapping(value = "login")
    public Result login(LoginDto loginDto) {
        Subject subject = SecurityUtils.getSubject();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginDto.getUserName(), loginDto.getPassWord());
            subject.login(usernamePasswordToken);
            return Result.success(JWTUtils.createToken(loginDto.getUserName()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getCause());
        }
    }
}
